package web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entity.Book;
import entity.CarShop;

import service.BookService;
import util.web.ServletUtil;

public class ShopCar extends HttpServlet {

	
	private static final long serialVersionUID = 1L;


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		CarShop carShop = new CarShop();
		String id = request.getParameter("id");
		Book book = new BookService().findBookById(id);
		carShop.setBook(book);
		carShop.setNum(1);
		
		
		List<CarShop> list = (List<CarShop>) request.getSession().getAttribute("carshop");
		
		if (list == null) {
			list = new ArrayList<CarShop>();
			list.add(carShop);
			
		}else {
			boolean f = false;
			for (CarShop car : list) {
				if (car.getBook().getId().equals(id)) {
					car.setNum(car.getNum() + 1);
					f = true;
				}
			}
			if (!f) {
				list.add(carShop);
			}
		}
		
		ServletUtil.setsessionAttri(request, "carshop", list);
		ServletUtil.sendRedirect(response, "showcar.jspx");
		
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	}

	
	
}
